package com.oocl.aeat.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.oocl.aeat.pojo.Verify;
import com.oocl.aeat.service.VerifyService;
import com.oocl.aeat.service.factory.VerifyServiceFactory;
import com.oocl.aeat.util.IPUtil;
import com.oocl.aeat.util.JsonUtil;

/**
 * 对M端暴露的接口
 * 查询商户状态
 * @author GANAB
 *
 */
@WebServlet("/muser/state")
public class StateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger;
	private VerifyService verifyService;
    public StateServlet() {
    	verifyService = VerifyServiceFactory.getInstance();
    	logger = Logger.getLogger(StateServlet.class);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	response.setCharacterEncoding("utf-8");
    	String ip = IPUtil.getIpAddress(request);
    	PrintWriter out = response.getWriter();
    	String mid = request.getParameter("mid");
    	if(mid==null || "".equals(mid)){
    		request.setAttribute("error", "mid cannot null");
    		request.getRequestDispatcher("../error.jsp").forward(request, response);
    		return;
    	}
    	Verify verify = verifyService.queryVerifyLastlyByMid(mid);
    	if(verify!=null){
    		out.write(JsonUtil.getJsonFromObject(verify));
        	out.flush();
        	System.out.println("ip="+ip+", Mid="+mid+" query state.");
        	logger.info("ip="+ip+", Mid="+mid+" query state.");
    	}else{
        	request.setAttribute("error", "Cannot found this muser");
    		request.getRequestDispatcher("../error.jsp").forward(request, response);
    		return;
    	}
	}

}
